package com.taoyige.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.taoyige.R;
import com.taoyige.bean.URLs;
import com.taoyige.common.BitmapUtil;
import com.taoyige.common.DPPXUtil;
import com.taoyige.common.StringUtils;
import com.taoyige.common.UIHelper;

/**
 * 商品详情页详情图片Adapter类
 * 
 * @author peter
 * @version 1.0
 * @created 2014-11-26
 */
public class SlideImageGalleryAdapter extends BaseAdapter {
	private Context context;// 运行上下文
	private List<String> listItems;// 数据集合
	private LayoutInflater listContainer;// 视图容器
	private int itemViewResource;// 自定义项视图源
	private BitmapUtil bmpUtil;

	static class ListItemView { // 自定义控件集合
		public ImageView slideImage;
	}

	/**
	 * 实例化Adapter
	 * 
	 * @param context
	 * @param data
	 * @param resource
	 */
	public SlideImageGalleryAdapter(Context context, List<String> data, int resource) {
		this.context = context;
		this.listContainer = LayoutInflater.from(context); // 创建视图容器并设置上下文
		this.itemViewResource = resource;
		this.listItems = data;
		this.bmpUtil = new BitmapUtil(BitmapFactory.decodeResource(context.getResources(), 0));
	}

	public int getCount() {
		return listItems.size();
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}

	/**
	 * ListView Item设置
	 */
	public View getView(int position, View convertView, ViewGroup parent) {

		ListItemView listItemView = null;

		if (convertView == null) {
			convertView = listContainer.inflate(this.itemViewResource, null);
			
			listItemView = new ListItemView();
			listItemView.slideImage = (ImageView) convertView
					.findViewById(R.id.goods_detail_item_image_url_slide);

			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}


		String imageUrl = URLs.STATIC_HOST+listItems.get(position%listItems.size());
		
		if (StringUtils.isEmpty(imageUrl)) {
			listItemView.slideImage.setImageResource(R.drawable.demo_goods);
		} else {
			bmpUtil.loadBitmap(imageUrl, listItemView.slideImage);
		}
		listItemView.slideImage.setVisibility(View.VISIBLE);
		listItemView.slideImage.setScaleType(ScaleType.FIT_XY);
		//listItemView.slideImage.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, DPPXUtil.dp2px(context, 350)));
		
		
		
		return convertView;
	}
}